package me.goldze.mvvmhabit.binding.viewadapter.qmui

import android.content.res.ColorStateList
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import com.chengyi.serialport.demo.R
import com.qmuiteam.qmui.alpha.QMUIAlphaViewInf
import com.qmuiteam.qmui.widget.QMUISlider
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButtonDrawable
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundFrameLayout
import me.jessyan.autosize.utils.AutoSizeUtils

object ViewAdapter {
    /*****     QMUI    common  start       ****/
    @BindingAdapter(value = ["changeAlpha"])
    @JvmStatic
    fun setAlphaViewPressAndDisable(view: View, value: Boolean) {
        setAlphaViewPress(view, value)
        setAlphaViewDisable(view, value)
    }

    @BindingAdapter(value = ["changeAlphaWhenPress"])
    @JvmStatic
    fun setAlphaViewPress(view: View, value: Boolean) {
        (view as? QMUIAlphaViewInf)?.setChangeAlphaWhenPress(value)
    }

    @BindingAdapter(value = ["changeAlphaWhenDisable"])
    @JvmStatic
    fun setAlphaViewDisable(view: View, value: Boolean) {
        (view as? QMUIAlphaViewInf)?.setChangeAlphaWhenDisable(value)
    }

    @BindingAdapter(value = ["qmui_backgroundColor", "qmui_borderColor", "qmui_borderWidth"], requireAll = false)
    @JvmStatic
    fun setQmuiRoundStyle(view: QMUIRoundFrameLayout, bgColor: Int?, borderColor: Int?, borderWidth: Float?) {
        (view.background as? QMUIRoundButtonDrawable)?.let { drawable ->
            bgColor?.let {
                drawable.setBgData(ColorStateList.valueOf(it))
            }
            if (borderColor != null) {
                drawable.setStroke(if (borderWidth == null) drawable.strokeWidth else AutoSizeUtils.dp2px(view.context, borderWidth), ColorStateList.valueOf(borderColor))
            }
        }
    }
    /*****     QMUI    common  end       ****/

    /*****     QMUISlider,QMUISeekbar的双向绑定   start       ****/
    @JvmStatic
    @BindingAdapter("currentProgress")
    fun setCurrentProgress(view: QMUISlider, oldValue: Int, newValue: Int) {
        if (oldValue != newValue) {
            view.currentProgress = newValue
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "currentProgress", event = "onCurrentProgressChanged")
    fun getCurrentProgress(view: QMUISlider): Int {
        return view.currentProgress
    }

    @JvmStatic
    @BindingAdapter(value = ["callback", "onCurrentProgressChanged"], requireAll = false)
    fun setCurrentProgressChanged(view: QMUISlider, listener: QMUISlider.Callback?, attrChange: InverseBindingListener?) {
        if (attrChange == null) {
            view.setCallback(listener)
        } else {
            view.setCallback(object : QMUISlider.Callback {
                override fun onProgressChange(slider: QMUISlider?, progress: Int, tickCount: Int, fromUser: Boolean) {
                    listener?.onProgressChange(slider, progress, tickCount, fromUser)
                    attrChange.onChange()
                }

                override fun onTouchDown(slider: QMUISlider?, progress: Int, tickCount: Int, hitThumb: Boolean) {
                    listener?.onTouchDown(slider, progress, tickCount, hitThumb)
                }

                override fun onTouchUp(slider: QMUISlider?, progress: Int, tickCount: Int) {
                    listener?.onTouchUp(slider, progress, tickCount)

                }

                override fun onStartMoving(slider: QMUISlider?, progress: Int, tickCount: Int) {
                    listener?.onStartMoving(slider, progress, tickCount)
                }

                override fun onStopMoving(slider: QMUISlider?, progress: Int, tickCount: Int) {
                    listener?.onStopMoving(slider, progress, tickCount)
                }

                override fun onLongTouch(slider: QMUISlider?, progress: Int, tickCount: Int) {
                    listener?.onLongTouch(slider, progress, tickCount)
                }
            })
        }
    }
    /*****     QMUISlider,QMUISeekbar的双向绑定   end       ****/
    /*****     QMUICommonListItemView                      ****/
    @JvmStatic
    @BindingAdapter(value = ["text", "detailText", "orientation", "qmui_common_list_title_color", "qmui_common_list_title_size_dp","qmui_common_list_detail_color", "qmui_common_list_detail_size_dp"], requireAll = false)
    fun setQMUICommonListItemViewProp(view: QMUICommonListItemView, text: String?, detailText: String?, orientation: Int?, titleColor: Int?, titleSize: Float?, detailColor: Int?,detailSize: Float?) {
        text?.let(view::setText)
        detailText?.let(view::setDetailText)
        orientation?.let(view::setOrientation)
        if (titleColor != null) {
            view.findViewById<TextView>(R.id.group_list_item_textView)?.setTextColor(titleColor)
        }
        if (titleSize != null) {
            view.findViewById<TextView>(R.id.group_list_item_textView)?.setTextSize(TypedValue.COMPLEX_UNIT_DIP, titleSize)
        }
        if (detailColor != null) {
            view.findViewById<TextView>(R.id.group_list_item_detailTextView)?.setTextColor(detailColor)
        }
        if (detailSize != null) {
            view.findViewById<TextView>(R.id.group_list_item_detailTextView)?.setTextSize(TypedValue.COMPLEX_UNIT_DIP, detailSize)
        }
    }
}